using SqlSugar;

namespace Ellwood.Manage.MentApi.Utility.InitDatabaseExt;

/// <summary>
/// 初始化SqlSugar
/// </summary>
public static class InitSqlSugarExt
{

    /// <summary>
    /// 初始化SqlSugar并注入
    /// </summary>
    /// <param name="builder"></param>
    public static void InitSqlSugar(this WebApplicationBuilder builder)
    {
        // 数据库连接信息
        string? connectionStr = builder.Configuration.GetConnectionString("connStr");
        
        if (string.IsNullOrWhiteSpace(connectionStr))
        {
            throw new Exception("请配置数据库连接字符串");
        }

        ConnectionConfig connection = new ConnectionConfig()
        {
            ConnectionString = connectionStr,
            DbType = DbType.SqlServer,
            IsAutoCloseConnection = true,
            InitKeyType = InitKeyType.Attribute
        };

        builder.Services.AddScoped<ISqlSugarClient>(s =>
        {
            SqlSugarClient client = new SqlSugarClient(connection);
            client.Aop.OnLogExecuting = (s, p) =>
            {
                Console.WriteLine($"OnLogExecuting:输出Sql语句:{s}||参数为：{string.Join(",",p.Select(p=>p.Value))}");
            };
            client.Aop.OnExecutingChangeSql = (s, p) =>
            {
                Console.WriteLine($"OnExecutingChangeSql:输出Sql语句:{s}||参数为：{string.Join(",", p.Select(p => p.Value))}");
                return new KeyValuePair<string, SugarParameter[]>(s, p);
            };
            client.Aop.OnLogExecuted = (s, p) =>
            {
                Console.WriteLine($"OnLogExecuted:输出Sql语句:{s}||参数为：{string.Join(",",p.Select(p=>p.Value))}");
            };
            client.Aop.OnError = e =>
            {
                Console.WriteLine($"OnError:Sql语句执行错误:{e.Message}");
            };

            {
                //  配置对于数据库操作的过滤器--SqlSugar独有的
            }
            return client;
        });
    }

}